<?php

class SiteBase extends BaseSiteBase {

	public static function initRestOfBaseSubsystems() {
		parent::initRestOfBaseSubsystems();
	}

	public static function addBaseClasses() {
		parent::addBaseClasses();

		// felüldefiniáljuk a user osztályokat
		addClass('DBSiteUser', 'classes/db/classDBSiteUser.php');
		addClass('SiteUser', 'classes/business/classSiteUser.php');

		addClass('SiteHelper', 'classes/classSiteHelper.php');
		addClass('MainPage', 'pageMain.php');

		addClass('ParameterException', 'classes/helpers/classParameterException.php');

		addClass('SiteAlertHandler', 'classes/handlers/classSiteAlertHandler.php');

		addClass('ExtendedArticleHandler', "classes/handlers/classExtendedArticleHandler.php");
		addClass('ExtendedArticle', "classes/content/classExtendedArticle.php");
		addClass('ExtendedFolderedArticleListGenerator', 'classes/contentGenerators/classExtendedFolderedArticleListGenerator.php');

		addClass('Box', 'classes/box/classBox.php');
		addClass('BoxTab', 'classes/box/classBoxTab.php');
		addClass('BoxContentList', 'classes/box/classBoxContentList.php');
		addClass('BoxHandler', 'classes/handlers/classBoxHandler.php');

		addClass('LoginBox', 'classes/box/classLoginBox.php');
		addClass('LoginBoxTab', 'classes/box/classLoginBoxTab.php');
		addClass('LoginBoxContentList', 'classes/box/classLoginBoxContentList.php');

		addClass('RegistrationPageGenerator', 'contentGenerators/classRegistrationPageGenerator.php');
		addClass('LostPasswordPageGenerator', 'contentGenerators/classLostPasswordPageGenerator.php');

		addClass('TournamentRegistrationBox', 'classes/box/classTournamentRegistrationBox.php');
		addClass('TournamentRegistrationBoxTab', 'classes/box/classTournamentRegistrationBoxTab.php');
		addClass('TournamentRegistrationBoxContentList', 'classes/box/classTournamentRegistrationBoxContentList.php');

		addClass('TournamentHandler', 'classes/handlers/classTournamentHandler.php');
		addClass('TournamentArchivePageGenerator', 'contentGenerators/classTournamentArchivePageGenerator.php');
		addClass('TournamentMonthPageGenerator', 'contentGenerators/classTournamentMonthPageGenerator.php');
		addClass('TournamentPageGenerator', 'contentGenerators/classTournamentPageGenerator.php');
		addClass('PartnerFinderGenerator', 'contentGenerators/classPartnerFinderGenerator.php');

		addClass('BridgeContentUrlManager', 'classes/navigation/classBridgeContentUrlManager.php');

		addClass('KBBOCalculation', 'classes/calculators/classKBBOCalculation.php');
	}

	/**
	 * user kilép/belép kezelése
	 * 
	 * A belépett userrel kapcsolatosan a továbbiakban a UserHandler osztállyal lehet majd
	 * műveleteket végezni. Pl. megtudni hogy kicsoda ő, milyen jogokkal, stb.
	 * 
	 * Sikeres művelet esetén a metódus egyből redirect-et csinál
	 *
	 * @throws Exception a hibákat nyelvesített exception-ben dobja
	 */
	public static function manageLoginLogout() {
		if(FunctionLibrary::getGetBoolValueSmart('loggedOut', false)) {
			// a session újraindult, megvolt a logout
			// Újabb redirect-et fogunk csinálni, de itt lehet customizálni...
			EventCenter::dispatchEvent(new Event(HydraEvents::SITE_USER_LOGGED_OUT));
			$url = UrlUtil::removeRequestParametersFromUrl(UrlUtil::getRequestUrl(), array("loggedOut"));
			FunctionLibrary::redirect($url);
			exit();
		}
		
		$username = null;
		$password = null;
		$viaCookie = false;
		$isMD5 = false;

		// pár dologban a cookie úgy működjön, ahogy a session cookie!
		$cNameUser = ConstHandler::get('COOKIE_USERNAME', null);
		$cNamePwd = ConstHandler::get('COOKIE_PASSWORD', null);
		$cookiesEnabled = !empty($cNameUser);
		$cPath = "/";
		$cDomain = "";
		$cSecure = false;
		$cHttpOnly = true;
		
		$viaAdminInterface = FunctionLibrary::getSessionValue('_viaAdminIface', false);
		if($viaAdminInterface)
			$cookiesEnabled = false;
		
		if(!UserHandler::hasUser() && $cookiesEnabled) {
			// nincs még loginolt user!
			// nézzük a cookie-t!
			if(isset($_COOKIE[$cNameUser])) {
				// van user cookie!
				$username = $_COOKIE[$cNameUser];
				$password = $_COOKIE[$cNamePwd];
				
				Log::debug("login cookie found! $username:$password");
				
				$rememberMe = "1";
				$viaCookie = true;
				$isMD5 = true;
			}
		}

		if (FunctionLibrary::getPostValue('loginformsubmit') || !is_null($username)) {
			
			// el lett küldve a form
			if(is_null($username))
				$username = FunctionLibrary::getPostValue('username');
			if(is_null($password))
				$password = FunctionLibrary::getPostValue('password');
			if(is_null($rememberMe)) {
				$rememberMe = FunctionLibrary::getPostValueSmart('rememberMe', null);
				if(is_null($rememberMe)) {
					if(FormUtil::getCheckboxRequestValue('rememberMeChkbox'))
						$rememberMe = "1";
				}
			}
			
			try {
				UserHandler::loginUser($username, $password, $isMD5, $viaCookie);

				// user logged in!
				Log::info("successfull login with $username!");
				
				if($rememberMe == "1" && $cookiesEnabled) {
					// mehet le a süti!
					$expire = time() + 86400 * 365;
					setcookie($cNameUser, $username, $expire, $cPath, $cDomain, $cSecure, $cHttpOnly);
					setcookie($cNamePwd, md5($password), $expire, $cPath, $cDomain, $cSecure, $cHttpOnly);
					
					Log::debug("rememberMe is active, cookies sent...");
				}

				// megjelöljük a session-ben, hogy a csávó éppen belépett
				$_SESSION['_justLoggedIn'] = true;

				FunctionLibrary::redirect(HydraLinkProvider::getLink('page'));
				exit();
			} catch (Exception $e) {
				// hiba
				
				if($viaCookie && $cookiesEnabled) {
					// törölni kell a cookie-kat mert szarok!
					setcookie($cNameUser, '', time() - 42000, $cPath, $cDomain, $cSecure, $cHttpOnly);
					setcookie($cNamePwd, '', time() - 42000, $cPath, $cDomain, $cSecure, $cHttpOnly);
				}
				
				throw $e;
			}
			
		} else if (FunctionLibrary::getGetOrPostValue('logout')) {
			$url = null;
			if(!is_null(SiteBase::$currentContentGenerator))
				$url = SiteBase::$currentContentGenerator->userWillLogout();
				
			// oké, mehet a logout!
			// ez majd kinyírja a session-t is konkrétan, új session indul majd!
			UserHandler::logout();
			
			// ezzel a cookie-kat is ki kell pucolni! kivéve, ha admin-on keresztül van jelen, akkor nem
			if($cookiesEnabled) {
				setcookie($cNameUser, '', time() - 42000, $cPath, $cDomain, $cSecure, $cHttpOnly);
				setcookie($cNamePwd, '', time() - 42000, $cPath, $cDomain, $cSecure, $cHttpOnly);
			}
			
			//FunctionLibrary::redirect(ContentUrlManager::getSiteUrl(SiteHandler::getCurrentSiteId(), "full", TextHandler::getCurrentLangcode()));
			if(is_null($url)) {
				$url = UrlUtil::removeRequestParametersFromUrl(UrlUtil::getRequestUrl(true), ConstHandler::get('REMOVE_REQ_PARAMS_AT_LOGOUT'));
			}
			$url = UrlUtil::addRequestParametersToUrl($url, array("loggedOut" => "1"));
			
			FunctionLibrary::redirect($url);
			exit();
		}
	}

}
?>